package com.leetcode.algorithm.y22.m09.w1;

/**
 * 1582. 二进制矩阵中的特殊位置
 * 
 * https://leetcode.cn/problems/special-positions-in-a-binary-matrix/
 * 
 * @author jie.deng
 *
 */
class Question1528Solution01 {

	public int numSpecial(int[][] mat) {
		int cnt = 0;
		int m = mat.length;
		int n = mat[0].length;
		int[] rowCnt = new int[m];
		int[] colCnt = new int[n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (mat[i][j] == 1) {
					rowCnt[i]++;
					colCnt[j]++;
					if (rowCnt[i] == 1 && colCnt[j] == 1) {
						for (int j2 = j + 1; j2 < n; j2++) {
							if (mat[i][j2] == 1) {
								rowCnt[i]++;
								colCnt[j2]++;
							}
						}
						for (int i2 = i + 1; i2 < m; i2++) {
							if (mat[i2][j] == 1) {
								rowCnt[i2]++;
								colCnt[j]++;
							}
						}
						if (rowCnt[i] == 1 && colCnt[j] == 1) {
							cnt++;
						}
					}
				}
			}
		}
		return cnt;
	}

}